package com.lgx.group.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.lgx.common.web.Constants;
import com.lgx.common.web.Pagination;
import com.lgx.mobile.service.ITopicService;
import com.lgx.mobile.vo.TopicVO;

@Controller
@RequestMapping("/topic")  
public class TopicController {
	@Autowired
	ITopicService topicService;
	
	public ITopicService getTopicService() {
		return topicService;
	}

	@RequestMapping("/topic.ftl")
	public String list(HttpServletRequest request,HttpServletResponse response){
		String path = request.getContextPath();
		String basePath = request.getScheme() + "://"
				+ request.getServerName() + ":" + request.getServerPort()
				+ path + "/";
		
		request.setAttribute("basePath", basePath);
		
		//热门话题列表
		int num = 7;
		List<TopicVO> hotTopicList = this.topicService.findHotTopicList(num);
		request.setAttribute("hotTopicList", hotTopicList);
		
		//话题列表(按类型分类)
		String pageNum = request.getParameter("page");
		String pageSize= request.getParameter("rows");
		
		if(StringUtils.isEmpty(pageSize))pageSize = "8";
		if(StringUtils.isEmpty(pageNum))pageNum = Constants.FIRST_PAGE_NUM;
		
		Object[] params = new Object[10];
		
//		long userId = 0;
//		User onlineUser = (User) request.getSession().getAttribute(Constants.LOGIN_USER);//得到当前登录人信息
//		if(onlineUser != null){
//			userId = onlineUser.getId();
//		}
		String categoryId = request.getParameter("categoryId");
		
		Pagination topicPageOfCategory = this.topicService.findTopicPaginationByCategoryId(Long.parseLong(categoryId), params, TopicVO.class, Integer.parseInt(pageSize), Integer.parseInt(pageNum));
		request.setAttribute("topicPageOfCategory", topicPageOfCategory);
		
		request.setAttribute("pageNum", pageNum);
		request.setAttribute("categoryId", categoryId);
		
		return "topic/topic";
	}
	
	@RequestMapping("/index.htm")
	public String index(HttpServletRequest request,HttpServletResponse response){
		return "topic/topic";
	}

	
	@RequestMapping("/topicSearch.htm")  
	public String userSearch(HttpServletRequest request,HttpServletResponse response) {
		String pageNum = request.getParameter("page");
		String categoryId = request.getParameter("categoryId");
		
		request.setAttribute("pageNum", pageNum);
		request.setAttribute("categoryId", categoryId);
		return "topic/topic";
	}
}
